/*
 * @(#)Vendor1.java   09/12/30
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package Example.Components;

import Example.Interfaces.Account;
import Example.Interfaces.Bike;
import Example.Interfaces.Main;

import org.objectweb.fractal.api.control.BindingController;

import com.sun.tools.javac.util.Name.Table;

import java.awt.Button;
import java.awt.Point;
import java.awt.Rectangle;

import java.util.Vector;

import javax.swing.table.TableColumn;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/12/30
 * @author         Huaxi (Yulin) ZHANG    
 */
public class Vendor1 implements Main, BindingController, Runnable {

    /** Field description */
    private Group BikeInfor_group = null;

    /** Field description */
    private Label accountRest_label = null;

    /** Field description */
    private Text accountRest_text = null;

    /** Field description */
    private Group account_group = null;

    /** Field description */
    private Label address_label = null;

    /** Field description */
    private Text address_text = null;

    /** Field description */
    private Label bikeIDrent_label = null;

    /** Field description */
    private Text bikeIDrent_text = null;

    /** Field description */
    private Label bikeIDreturn_label = null;

    /** Field description */
    private Text bikeIDreturn_text = null;

    /** Field description */
    private Table bikes_table = null;

    /** Field description */
    private Label cardName_Label = null;

    /** Field description */
    private Text cardName_Text = null;

    /** Field description */
    private Label cardPass_label = null;

    /** Field description */
    private Button check_button = null;

    /** Field description */
    private Label cost_label = null;

    /** Field description */
    private Text cost_text = null;

    /** Field description */
    private Label distance_label = null;

    /** Field description */
    private Text distance_text = null;

    /** Field description */
    private Button findBike_button = null;

    /** Field description */
    private Label locationEnd_label = null;

    /** Field description */
    private Text locationEnd_text = null;

    /** Field description */
    private Label locationR_label = null;

    /** Field description */
    private Text locationR_text = null;

    /** Field description */
    private Label location_label = null;

    /** Field description */
    private Text location_text = null;

    /** Field description */
    private Text password_text = null;

    /** Field description */
    private Button pay_button = null;

    /** Field description */
    private Group pay_group = null;

    /** Field description */
    private boolean rent = false;

    /** Field description */
    private Label rentBID_label = null;

    /** Field description */
    private Text rentBID_text = null;

    /** Field description */
    private Label rentState_label = null;

    /** Field description */
    private Button rent_button = null;

    /** Field description */
    private Group rent_group = null;

    /** Field description */
    private Text rentstate_text = null;

    /** Field description */
    private Button return_button = null;

    /** Field description */
    private Group return_group = null;

    /** Field description */
    private Shell sShell = null;    // @jve:decl-index=0:visual-constraint="11,11"

    /** Field description */
    private Label tripSum_label = null;

    /** Field description */
    private Text tripSum_text = null;

    /** Field description */
    private Label trip_label = null;

    /** Field description */
    private Text trip_text = null;

    /** Field description */
    private Text userFName_text = null;

    /** Field description */
    private Label userFname_label = null;

    /** Field description */
    private Group userInfor_group = null;

    /** Field description */
    private Label userLname_label = null;

    /** Field description */
    private Text userLname_text = null;

    /** Field description */
    private Account Account;    // @jve:decl-index=0:

    /** Field description */
    private Bike Bike;

    /** Field description */
    private Label rentTime_label;

    /** Field description */
    private Text rentTime_text;

    // user information

    /** Field description */
    private int userID;

    /** Field description */
    private Object[] userInfor;

    /** Field description */
    private String userPassword;

    // define required interfaces

    /**
     * Method description
     *
     * @return  
     */
    public String[] listFc() {
        return new String[] { "Account,Bike" };
    }

    /**
     * Method description
     *
     * @param cItf  
     * @return  
     */
    public Object lookupFc(String cItf) {
        if (cItf.equals("Account")) {
            return Account;
        }

        if (cItf.equals("Bike")) {
            return Bike;
        }

        return null;
    }

    /**
     * Method description
     *
     * @param cItf  
     * @param sItf  
     */
    public void bindFc(String cItf, Object sItf) {
        if (cItf.equals("Account")) {
            Account = (Account) sItf;
        }

        if (cItf.equals("Bike")) {
            Bike = (Bike) sItf;
        }
    }

    /**
     * Method description
     *
     * @param cItf  
     */
    public void unbindFc(String cItf) {
        if (cItf.equals("Account")) {
            Account = null;
        }

        if (cItf.equals("Bike")) {
            Bike = null;
        }
    }

    /**
     * Method description
     *
     */
    public void run() {

        // basket.rentB(789, 123);
        // TODO Auto-generated method stub
        Display display = Display.getDefault();
        Shell   sShell  = createSShell();

        sShell.open();

        while (!sShell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        // display.dispose();
    }

    /**
     * Method description
     *
     */
    private void createBikeInfor_group() {
        BikeInfor_group = new Group(sShell, SWT.NONE);
        BikeInfor_group.setText("Bike Information");
        BikeInfor_group.setLayout(null);
        BikeInfor_group.setBounds(new Rectangle(323, 111, 241, 151));
        bikes_table = new Table(BikeInfor_group, SWT.BORDER | SWT.V_SCROLL);
        bikes_table.setHeaderVisible(true);
        bikes_table.setLinesVisible(true);
        bikes_table.setBounds(new Rectangle(8, 17, 226, 124));

        final TableColumn ct1 = new TableColumn(bikes_table, SWT.NONE);

        ct1.setWidth(60);
        ct1.setText("ID");

        final TableColumn ct2 = new TableColumn(bikes_table, SWT.NONE);

        ct2.setWidth(60);
        ct2.setText("Trip");

        final TableColumn ct3 = new TableColumn(bikes_table, SWT.NONE);

        ct3.setWidth(102);
        ct3.setText("Location");
    }

    /**
     * This method initializes userInfor_group
     *
     */
    private void createUserInfor_group() {
        userInfor_group = new Group(sShell, SWT.NONE);
        userInfor_group.setText("User Information");
        userInfor_group.setLayout(null);
        userInfor_group.setBounds(new Rectangle(14, 83, 252, 112));
        userFname_label = new Label(userInfor_group, SWT.NONE);
        userFname_label.setText("First Name");
        userFname_label.setBounds(new Rectangle(8, 21, 51, 13));
        userFName_text = new Text(userInfor_group, SWT.BORDER);
        userFName_text.setBounds(new Rectangle(64, 18, 174, 19));
        userLname_label = new Label(userInfor_group, SWT.NONE);
        userLname_label.setText("Last Name");
        userLname_label.setBounds(new Rectangle(8, 45, 50, 13));
        userLname_text = new Text(userInfor_group, SWT.BORDER);
        userLname_text.setBounds(new Rectangle(64, 42, 174, 19));
        address_label = new Label(userInfor_group, SWT.NONE);
        address_label.setText("Address");
        address_label.setBounds(new Rectangle(8, 69, 39, 13));
        address_text = new Text(userInfor_group, SWT.BORDER);
        address_text.setBounds(new Rectangle(64, 66, 174, 38));
    }

    /**
     * This method initializes account_group
     *
     */
    private void createAccount_group() {
        account_group = new Group(sShell, SWT.NONE);
        account_group.setText("Account Information");
        account_group.setLayout(null);
        account_group.setBounds(new Rectangle(14, 208, 254, 143));
        accountRest_label = new Label(account_group, SWT.NONE);
        accountRest_label.setText("Account rest:");
        accountRest_label.setBounds(new Rectangle(7, 20, 68, 17));
        accountRest_text = new Text(account_group, SWT.BORDER);
        accountRest_text.setBounds(new Rectangle(76, 21, 166, 20));
        rentState_label = new Label(account_group, SWT.NONE);
        rentState_label.setBounds(new Rectangle(7, 45, 68, 17));
        rentState_label.setText("Rent_state:");
        rentstate_text = new Text(account_group, SWT.BORDER);
        rentstate_text.setBounds(new Rectangle(76, 45, 166, 20));
        tripSum_label = new Label(account_group, SWT.NONE);
        tripSum_label.setBounds(new Rectangle(7, 70, 68, 17));
        tripSum_label.setText("TripSum:");
        tripSum_text = new Text(account_group, SWT.BORDER);
        tripSum_text.setBounds(new Rectangle(76, 69, 166, 20));
        rentBID_label = new Label(account_group, SWT.NONE);
        rentBID_label.setBounds(new Rectangle(7, 95, 68, 17));
        rentBID_label.setText("RentBike:");
        rentBID_text = new Text(account_group, SWT.BORDER);
        rentBID_text.setBounds(new Rectangle(76, 93, 166, 20));
        rentTime_label = new Label(account_group, SWT.NONE);
        rentTime_label.setBounds(new Rectangle(7, 120, 68, 17));
        rentTime_label.setText("Rent time:");
        rentTime_text = new Text(account_group, SWT.BORDER);
        rentTime_text.setBounds(new Rectangle(76, 117, 166, 20));
    }

    /**
     * Method description
     *
     */
    private void createReturn_group() {
        return_group = new Group(sShell, SWT.NONE);
        return_group.setText("Return");
        return_group.setLayout(null);
        return_group.setBounds(new Rectangle(323, 311, 251, 92));
        bikeIDreturn_label = new Label(return_group, SWT.NONE);
        bikeIDreturn_label.setText("BikeID:");
        bikeIDreturn_label.setBounds(new Rectangle(7, 21, 35, 15));
        bikeIDreturn_text = new Text(return_group, SWT.BORDER);
        bikeIDreturn_text.setBounds(new Rectangle(53, 18, 126, 19));

        Label filler = new Label(return_group, SWT.NONE);

        filler.setBounds(new Rectangle(123, 21, 0, 13));
        trip_label = new Label(return_group, SWT.NONE);
        trip_label.setText("Trip:");
        trip_label.setBounds(new Rectangle(7, 47, 35, 15));
        trip_text = new Text(return_group, SWT.BORDER);
        trip_text.setBounds(new Rectangle(53, 44, 127, 19));
        locationR_label = new Label(return_group, SWT.NONE);
        locationR_label.setText("Loc:");
        locationR_label.setBounds(new Rectangle(7, 73, 35, 15));
        locationR_text = new Text(return_group, SWT.BORDER);
        locationR_text.setBounds(new Rectangle(53, 70, 127, 19));
        return_button = new Button(return_group, SWT.NONE);
        return_button.setText("Return");
        return_button.setBounds(new Rectangle(190, 42, 45, 23));
        return_button.setEnabled(false);
        return_button.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                int    bikeID   = Integer.parseInt(bikeIDreturn_text.getText());
                String location = locationR_text.getText();

                System.out.println(System.currentTimeMillis());

                double trip = Double.parseDouble(trip_text.getText());

                Bike.returnB(userID, bikeID, location, trip);
            }
            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {}
        });
    }

    /**
     * This method initializes sShell
     * @return  
     */
    private Shell createSShell() {
        sShell = new Shell();
        sShell.setSize(new Point(633, 476));
        sShell.setText("Rent Bike System");
        sShell.setSize(new Point(630, 438));
        sShell.setLayout(null);
        cardName_Text = new Text(sShell, SWT.BORDER);
        cardName_Text.setBounds(new Rectangle(76, 15, 169, 18));
        cardName_Label = new Label(sShell, SWT.NONE);
        cardName_Label.setBounds(new Rectangle(15, 15, 61, 18));
        cardName_Label.setText("Card ID:");
        password_text = new Text(sShell, SWT.BORDER);
        password_text.setBounds(new Rectangle(76, 39, 169, 16));
        check_button = new Button(sShell, SWT.NONE);
        check_button.setBounds(new Rectangle(16, 59, 58, 20));
        check_button.setText("CheckID");
        check_button.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                System.out.println("login");    // TODO Auto-generated Event stub widgetSelected()

                String a = cardName_Text.getText();

                userPassword = password_text.getText();
                userID       = Integer.parseInt(a);
                System.out.println("h111");
                userInfor = Account.login(userID, userPassword);
                System.out.println("h2222");
                userFName_text.setText(userInfor[2].toString());
                userLname_text.setText(userInfor[3].toString());
                address_text.setText(userInfor[4].toString());
                accountRest_text.setText(userInfor[5].toString());
                tripSum_text.setText(userInfor[6].toString());
                rentTime_text.setText(userInfor[9].toString());

                if (userInfor[7].toString().equals("true")) {
                    rentstate_text.setText("under rent");
                    rent_button.setEnabled(false);
                    return_button.setEnabled(true);
                } else {
                    rentstate_text.setText("no rent");
                    rent_button.setEnabled(true);
                    return_button.setEnabled(false);
                }

                if (!userInfor[8].toString().equals("0")) {
                    rentBID_text.setText(userInfor[8].toString());
                } else {
                    rentBID_text.setText("no rent");
                }
            }
            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {
                ;
            }
        });
        createBikeInfor_group();
        createUserInfor_group();
        createAccount_group();
        location_label = new Label(sShell, SWT.NONE);
        location_label.setBounds(new Rectangle(308, 15, 74, 18));
        location_label.setText("Location start: ");
        location_text = new Text(sShell, SWT.BORDER);
        location_text.setBounds(new Rectangle(382, 15, 126, 18));
        locationEnd_label = new Label(sShell, SWT.NONE);
        locationEnd_label.setBounds(new Rectangle(308, 45, 74, 18));
        locationEnd_label.setText("Location end:");
        locationEnd_text = new Text(sShell, SWT.BORDER);
        locationEnd_text.setBounds(new Rectangle(382, 45, 126, 18));
        distance_label = new Label(sShell, SWT.NONE);
        distance_label.setBounds(new Rectangle(308, 75, 74, 18));
        distance_label.setText("Distance:");
        distance_text = new Text(sShell, SWT.BORDER);
        distance_text.setBounds(new Rectangle(382, 75, 126, 18));
        findBike_button = new Button(sShell, SWT.NONE);
        findBike_button.setBounds(new Rectangle(516, 15, 81, 20));
        findBike_button.setText("Find bikes");
        findBike_button.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                System.out.println("findBike");    // TODO Auto-generated Event stub widgetSelected()

                Vector bikes = new Vector();

                bikes = Bike.findBike(location_text.getText(), locationEnd_text.getText());

                int i = bikes.size();

                distance_text.setText((String) bikes.get(i - 1));

                for (int j = 0; j < bikes.size() - 1; j++) {
                    Object[]  b    = (Object[]) bikes.get(j);
                    TableItem item = new TableItem(bikes_table, SWT.NONE);

                    item.setText(new String[] { b[0].toString(), b[1].toString(), b[2].toString() });
                }
            }
            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {}
        });
        cardPass_label = new Label(sShell, SWT.NONE);
        cardPass_label.setBounds(new Rectangle(15, 38, 61, 18));
        cardPass_label.setText("Password:");
        createReturn_group();
        createRent_group();
        createPay_group();

        return sShell;
    }

    /**
     * This method initializes rent_group
     *
     */
    private void createRent_group() {
        rent_group = new Group(sShell, SWT.NONE);
        rent_group.setText("Rent");
        rent_group.setLayout(null);
        rent_group.setBounds(new Rectangle(323, 262, 252, 43));
        bikeIDrent_label = new Label(rent_group, SWT.NONE);
        bikeIDrent_label.setText("BikeID:");
        bikeIDrent_label.setBounds(new Rectangle(7, 17, 35, 15));
        rent_button = new Button(rent_group, SWT.NONE);
        rent_button.setText("Rent");
        rent_button.setBounds(new Rectangle(192, 17, 35, 20));
        rent_button.setEnabled(false);
        rent_button.addSelectionListener(new org.eclipse.swt.events.SelectionListener() {
            public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
                int i;

                i = Integer.valueOf(bikeIDrent_text.getText()).intValue();
                Bike.rentB(i, userID);
            }
            public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) {}
        });
        bikeIDrent_text = new Text(rent_group, SWT.BORDER);
        bikeIDrent_text.setBounds(new Rectangle(52, 16, 127, 19));
    }

    /**
     * This method initializes pay_group
     *
     */
    private void createPay_group() {
        pay_group = new Group(sShell, SWT.NONE);
        pay_group.setLayout(null);
        pay_group.setBounds(new Rectangle(15, 350, 256, 43));
        pay_button = new Button(pay_group, SWT.NONE);
        pay_button.setBounds(new Rectangle(199, 12, 39, 23));
        pay_button.setText("Pay");
        cost_text = new Text(pay_group, SWT.BORDER);
        cost_text.setBounds(new Rectangle(65, 16, 125, 18));
        cost_label = new Label(pay_group, SWT.NONE);
        cost_label.setBounds(new Rectangle(9, 15, 52, 19));
        cost_label.setText("Cost:");
    }
}
